package com.ruoyi.category.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.category.domain.StockGoods;
import com.ruoyi.category.mapper.StockCategoryMapper;
import com.ruoyi.category.domain.StockCategory;
import com.ruoyi.category.service.IStockCategoryService;

/**
 * 商品类别管理Service业务层处理
 * 
 * @author yx
 * @date 2024-12-19
 */
@Service
public class StockCategoryServiceImpl implements IStockCategoryService 
{
    @Autowired(required = false)
    private StockCategoryMapper stockCategoryMapper;

    /**
     * 查询商品类别管理
     * 
     * @param categoryId 商品类别管理主键
     * @return 商品类别管理
     */
    @Override
    public StockCategory selectStockCategoryByCategoryId(Integer categoryId)
    {
        return stockCategoryMapper.selectStockCategoryByCategoryId(categoryId);
    }

    /**
     * 查询商品类别管理列表
     * 
     * @param stockCategory 商品类别管理
     * @return 商品类别管理
     */
    @Override
    public List<StockCategory> selectStockCategoryList(StockCategory stockCategory)
    {
        return stockCategoryMapper.selectStockCategoryList(stockCategory);
    }

    /**
     * 新增商品类别管理
     * 
     * @param stockCategory 商品类别管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStockCategory(StockCategory stockCategory)
    {
        int rows = stockCategoryMapper.insertStockCategory(stockCategory);
        insertStockGoods(stockCategory);
        return rows;
    }

    /**
     * 修改商品类别管理
     * 
     * @param stockCategory 商品类别管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStockCategory(StockCategory stockCategory)
    {
        stockCategoryMapper.deleteStockGoodsByGoodsCategory(stockCategory.getCategoryId());
        insertStockGoods(stockCategory);
        return stockCategoryMapper.updateStockCategory(stockCategory);
    }

    /**
     * 批量删除商品类别管理
     * 
     * @param categoryIds 需要删除的商品类别管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStockCategoryByCategoryIds(Integer[] categoryIds)
    {
        stockCategoryMapper.deleteStockGoodsByGoodsCategorys(categoryIds);
        return stockCategoryMapper.deleteStockCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除商品类别管理信息
     * 
     * @param categoryId 商品类别管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStockCategoryByCategoryId(Integer categoryId)
    {
        stockCategoryMapper.deleteStockGoodsByGoodsCategory(categoryId);
        return stockCategoryMapper.deleteStockCategoryByCategoryId(categoryId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param stockCategory 商品类别管理对象
     */
    public void insertStockGoods(StockCategory stockCategory)
    {
        List<StockGoods> stockGoodsList = stockCategory.getStockGoodsList();
        Integer categoryId = stockCategory.getCategoryId();
        if (StringUtils.isNotNull(stockGoodsList))
        {
            List<StockGoods> list = new ArrayList<StockGoods>();
            for (StockGoods stockGoods : stockGoodsList)
            {
                stockGoods.setGoodsCategory(String.valueOf(categoryId));
                list.add(stockGoods);
            }
            if (list.size() > 0)
            {
                stockCategoryMapper.batchStockGoods(list);
            }
        }
    }
}
